<?php
namespace clt;
use think\Config;
use think\Session;
class Auth{
	public function __construct() {
		define('MODULE_NAME',strtolower(request()->controller()));
        define('ACTION_NAME',strtolower(request()->action()));
        $this->current_uid = Session::get('aid');
        $this->verifyAuth();
	}
	/*验证权限*/
	public function verifyAuth(){
		/*超级管理员，不需要继续验证*/
		if($this->current_uid == 1){
			return true;
		}
		/*验证是否需要验证该路径*/
		$currRuleId = db('AuthRule')->where(['authopen'=>1,'href'=>str_replace("\\", "/", MODULE_NAME.DS.ACTION_NAME)])->value('id');
		if(intval($currRuleId) <= 0){
			return true;
		}
		$rules = db('users')->alias('a')
				->join('AuthGroup m',"a.group_id = m.group_id")
				->where('a.admin_id',$this->current_uid)
				->value('m.rules');
		if(empty($rules)){
			return false;
		}
		$rules = explode(",", $rules);
		if(in_array($currRuleId, $rules)){
			return true;
		}
		return false;
	}
}